package com.hjs.juc;

import java.util.concurrent.CountDownLatch;

/**
 * 闭锁：在完成某些运算时，只有其他所有的线程运行完，当前运算才能完成
 *
 *
 * @author hanjinsong
 * @date 2020-02-19 14:05
 */
public class TestCountDownLatch {

    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(5);
        LatchDemo latchDemo = new LatchDemo(latch);
        long start = System.currentTimeMillis();

        for (int i = 0; i < 5; i++) {
            new Thread(latchDemo).start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗费了:" + (end-start));

    }
}

class LatchDemo implements Runnable {

    private CountDownLatch latch;

    public LatchDemo(CountDownLatch latch){
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 50000; i++) {
                if (i % 2 ==0 ) {
                    System.out.println(i);
                }
            }
        } finally {
            latch.countDown();
        }

    }
}